package com.paomi.client.bean;


import com.paomi.client.utils.SPUtil;

/**
 * Created by LS on 2017/6/5 0005.
 * 用户信息缓存工具
 */

public class UserUtils {
    public static final String USER_INFO = "user_info";//含有token的对象
    public static final String USER_INFO1 = "user_data";//含有整个对象

    private UserInfo userInfo;
    private UserInfo userInfo1;

    private UserUtils(){}

    private static class SingletonHolder{
        private static final UserUtils instance = new UserUtils();
    }

    public static UserUtils getInstance() {
        return SingletonHolder.instance;
    }

    public UserInfo getUserInfo() {
        if(userInfo != null) return userInfo;
        try {
            userInfo = SPUtil.getObjectFromShare(USER_INFO);
        }catch (Exception e){
            e.printStackTrace();
        }

        if(userInfo==null) return null;
        return this.userInfo;
    }

    public UserInfo saveUser(UserInfo userInfo) {
        this.userInfo = userInfo;

        SPUtil.saveObjectToShare(USER_INFO, userInfo);
        return this.userInfo;
    }
    public UserInfo getUserInfo1() {
        if(userInfo1 != null) return userInfo1;
        try {
            userInfo1 = SPUtil.getObjectFromShare(USER_INFO1);
        }catch (Exception e){
            e.printStackTrace();
        }

        if(userInfo1==null) return null;
        return this.userInfo1;
    }

    public UserInfo saveUser1(UserInfo userInfo) {
        this.userInfo1 = userInfo;

        SPUtil.saveObjectToShare(USER_INFO1, userInfo);
        return this.userInfo1;
    }

    public void clearUser() {
        SPUtil.saveObjectToShare(USER_INFO, null);
        SPUtil.saveObjectToShare(USER_INFO1, null);
        this.userInfo = null;
        this.userInfo1 = null;
    }
}
